ページレポート/RDLレポートではファイルパスを使用してファイルシステム上のリソースを取得できますが、ときどき、データベースなどの特殊なソースにリソースが保存されていることがあります。ページレポート/RDLレポートでは、カスタムリソースロケーターを使用して、レポートに必要なリソースをどのような場所からも読み込むことができます。このウォークスルーは「カスタムリソースロケーター」のサンプルを基にしており、ユーザーのMy Picturesディレクトリから画像をロードする方法を示します。
このウォークスルーは以下の手順に分かれています。
- ActiveReportsをVisual Studioプロジェクトに追加する
- レポートのレイアウトを作成する
- 新しいMyPicturesLocatorクラスを追加する
- レポートを表示するViewerコントロールを含むPreviewFormを作成する
このウォークスルーを完了したとき、実行時のレイアウトは以下のようになります。
 |
メモ: このチュートリアルではページレポートを使用していますが、RDLレポートを使用した場合も同様の手順で作成することが可能です。 |
ActiveReportsをVisual Studioプロジェクトに追加する
- Visual Studioで新しいWindowsフォームアプリケーションプロジェクトを作成します。
- [プロジェクト]メニューから[新しい項目の追加]を選択します。
- [新しい項目の追加]ダイアログが表示されたら、[ActiveReports 9.0J ページレポート]を選択し、[名前]フィールドでファイル名を「DemoReport.rdlx」に変更します。
- [追加]ボタンをクリックし、新しいページレポートを開きます。
レポートのレイアウトを作成する
- ツールボックスからImage コントロールをデザイナ面にドラッグし、 [プロパティ]ウィンドウで以下のプロパティを設定します。
プロパティ名 |
プロパティ値 |
Name |
Image1 |
Location |
0.1in, 0.1in |
Size |
2.8in, 2.8in |
Value |
MyPictures:Penguins.jpg |
- ツールボックスからImage コントロールをもう1つデザイナ面にドラッグし、 [プロパティ]ウィンドウで以下のプロパティを設定します。
プロパティ名 |
プロパティ値 |
Name |
Image2 |
Location |
3.1in, 0.1in |
Size |
2.8in, 2.8in |
Value |
MyPictures:Desert.jpg |
- ソリューションエクスプローラでDemoReport.rdlxを選択し、[プロパティ]ウィンドウで[ビルドアクション]を[埋め込まれたリソース]に設定します。
新しいMyPicturesLocatorクラスを追加する
- ソリューションエクスプローラで、プロジェクト名を右クリックして[追加] - [新しい項目]を選択します。
- [新しい項目の追加]ダイアログが表示されたら、[クラス]を選択します。
- クラスの名前をMyPicturesLocatorに変更し、[追加]ボタンをクリックします。
- 新しいクラスの既存のコードを以下のコードに置き換えます。
Visual Basic.NETでコードを記述する場合
VBのコード。一番上に貼り付けます。 |
コードのコピー
|
Imports System
Imports System.Drawing
Imports GrapeCity.ActiveReports.Extensibility
Imports System.Globalization
Imports System.IO
Imports System.Runtime.InteropServices
|
VBのコード。クラス内に貼り付けます。 |
コードのコピー
|
Inherits ResourceLocator
Private Const UriSchemeMyImages As String = "MyPictures:"
' リソースを取得して返します。
Public Overrides Function GetResource(resourceInfo As ResourceInfo) As Resource
Dim name As String = resourceInfo.Name
If name Is Nothing OrElse name.Length = 0 Then
Throw New ArgumentException("The name of resource to be obtained should be non-empty string.", "name")
End If
Dim uri As New Uri(name)
Dim stream As Stream = GetPictureFromSpecialFolder(name)
If stream Is Nothing Then
stream = New MemoryStream()
End If
Return New Resource(stream, uri)
End Function
' Public Picturesフォルダーから指定した画像を返します。
Private Shared Function GetPictureFromSpecialFolder(path As String) As Stream
Dim startPathPos As Integer = UriSchemeMyImages.Length
If startPathPos >= path.Length Then
Return Nothing
End If
Dim pictureName As String = path.Substring(startPathPos)
Dim myPicturesPath As String = Environment.GetEnvironmentVariable("public") & "\Pictures\Sample Pictures"
If Not myPicturesPath.EndsWith("\") Then
myPicturesPath += "\"
End If
Dim picturePath As String = System.IO.Path.Combine(myPicturesPath, pictureName)
If Not File.Exists(picturePath) Then
Return Nothing
End If
Dim stream As New MemoryStream()
Try
Dim picture As Image = Image.FromFile(picturePath)
picture.Save(stream, picture.RawFormat)
stream.Position = 0
Catch generatedExceptionName As OutOfMemoryException
' ファイルが有効な画像でないか、GDI+がこの画像をサポートしていません。
Return Nothing
Catch generatedExceptionName As ExternalException
Return Nothing
End Try
Return stream
End Function
|
C#でコードを記述する場合
C#コード。一番上に貼り付けます。 |
コードのコピー
|
using System;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using GrapeCity.ActiveReports.Extensibility;
using your_project_name.Properties;
|
C#コード。Usingステートメントの下に貼り付けます。 |
コードのコピー
|
namespace your_project_name
{
// My Picturesフォルダーでリソースを検索します。
internal sealed class MyPicturesLocator : ResourceLocator
{
private const string UriSchemeMyImages = "MyPictures:";
// リソースを取得して返します。
public override Resource GetResource(ResourceInfo resourceInfo)
{
string name = resourceInfo.Name;
if (name == null || name.Length == 0)
{
throw new ArgumentException("The name of resource to be obtained should be non-empty string.", "name");
}
Uri uri = new Uri(name);
Stream stream = GetPictureFromSpecialFolder(name);
if (stream == null)
{
stream = new MemoryStream();
}
return new Resource(stream, uri);
}
// Public Picturesフォルダーから指定した画像を返します。
private static Stream GetPictureFromSpecialFolder(string path)
{
int startPathPos = UriSchemeMyImages.Length;
if (startPathPos >= path.ToString().Length)
{
return null;
}
string pictureName = path.ToString().Substring(startPathPos);
string myPicturesPath = Environment.GetEnvironmentVariable("public") + "\\Pictures\\Sample Pictures";
if (!myPicturesPath.EndsWith("\\")) myPicturesPath += "\\";
string picturePath = Path.Combine(myPicturesPath, pictureName);
if (!File.Exists(picturePath)) return null;
MemoryStream stream = new MemoryStream();
try
{
Image picture = Image.FromFile(picturePath);
picture.Save(stream, picture.RawFormat);
stream.Position = 0;
}
catch (OutOfMemoryException) // ファイルが有効な画像でないか、GDI+がこの画像をサポートしていません。
{
return null;
}
catch (ExternalException)
{
return null;
}
return stream;
}
}
}
|
PreviewFormを作成する
- デザインビューでForm1を選択し、 [プロパティ]ウィンドウでプロパティを以下のように設定します。
プロパティ名 |
プロパティ値 |
Name |
PreviewForm |
Text |
プレビューフォーム |
Size |
1015, 770 |
- Visual StudioのツールボックスからViewerコントロールをドラッグしてPreviewFormにドロップし、 [プロパティ]ウィンドウで以下のプロパティを設定します。
プロパティ名 |
プロパティ値 |
Name |
reportPreview1 |
Dock |
Fill |
- PreviewFormをダブルクリックしてLoadイベントのインスタンスを作成し、以下のコードを追加します。
Visual Basic.NETでコードを記述する場合
VBのコード。Importsステートメントの下に貼り付けます。 |
コードのコピー
|
Imports GrapeCity.ActiveReports.Document
Imports System.IO
Imports GrapeCity.ActiveReports
|
VBのコード。Loadイベント内に貼り付けます。 |
コードのコピー
|
Dim reportData As Stream = [GetType]().Assembly.GetManifestResourceStream("your_project_name.DemoReport.rdlx")
reportData.Position = 0
Dim reader As New StreamReader(reportData)
Dim def As New PageReport(reader)
def.ResourceLocator = New MyPicturesLocator()
Dim runtime As New PageDocument(def)
reportPreview1.ReportViewer.LoadDocument(runtime)
|
C#でコードを記述する場合
C#コード。Usingステートメントの下に貼り付けます。 |
コードのコピー
|
using GrapeCity.ActiveReports.Document;
using System.IO;
using GrapeCity.ActiveReports;
|
C#コード。Loadイベント内に貼り付けます。 |
コードのコピー
|
string myPicturesPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
Stream reportData = GetType().Assembly.GetManifestResourceStream("your_project_name.DemoReport.rdlx");
reportData.Position = 0;
StreamReader reader = new StreamReader(reportData);
PageReport def = new PageReport(reader);
def.ResourceLocator = new MyPicturesLocator();
PageDocument runtime = new PageDocument(def);
reportPreview1.ReportViewer.LoadDocument(runtime);
|
- [F5]を押してプロジェクトを実行します。
関連トピック